package fm.dongman.animefm.view.fragment

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import fm.dongman.animefm.R
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.LoginContract
import fm.dongman.animefm.view.activity.SingleActivity
import fm.dongman.animefm.view.fragment.MyFragment.Companion.INTENT_LOGIN
import fm.dongman.animefm.view.fragment.MyFragment.Companion.KEY_USER_INFO
import fm.dongman.animefm.view.util.ActivityHelper
import fm.dongman.animefm.view.util.SystemHelper
import fm.dongman.animefm.view.util.ToastHelper
import fm.dongman.modellib.StaticArea.Injection
import fm.dongman.modellib.source.LoginRepository
import fm.dongman.presenterlib.presenter.LoginPresenter
import kotlinx.android.synthetic.main.fragment_login.*
import org.jetbrains.anko.enabled
import org.jetbrains.anko.onClick

/**
 * 登录页面
 * Created by shize on 2017/10/13.
 */
class LoginFragment : BaseFragment(), LoginContract.ILoginView {
    private lateinit var mPresenter: LoginContract.ILoginPresenter // 数据控制器
    private var mIsWXLogin = false // 针对微信登录的标识

    private lateinit var mDialog: Dialog //加载中弹窗

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val fragment = inflater?.inflate(R.layout.fragment_login, container, false)
        // 初始化控制器
        LoginPresenter(this, Injection.provideLoginRepository(container!!.context))
        return fragment!!
    }

    override fun initData() {

        mDialog=Dialog(activity,R.style.progress_dialog)
        mDialog.setContentView(R.layout.layout_loading_dialog)
    }

    override fun bindData() {
        mTbLogin.setPadding(0, SystemHelper.getStatusBarHeight(activity), 0, 0)
        bindButtonClick()     //My页面控件事件初始化
    }

    //my页面控件
    private fun bindButtonClick() {
        //注册
        mTvRegister.setOnClickListener {
            ActivityHelper.startActivity(activity, SingleActivity::class.java, mapOf(
                    Pair(SingleActivity.EXTRA_NAME, SingleActivity.INTENT_EXTRA_REGISTER),
                    Pair(SingleActivity.BUNDLE_EXTRA_KEY_TYPE, RegisterFragment.TYPE_REGISTER.toString())),
                    false)
//            mPresenter.register()
        }
        //登录事件
        mBtnLogin.setOnClickListener {
            notEnabledLoginBtn()
            if (!mPresenter.login(mEtUserName.text.toString(), mEtPassword.text.toString())) {
                ToastHelper.getInstance(activity).showWarn("账号或密码格式不正确")
                enabledLoginBtn()
            }
        }
        //QQ登陆
        mIvQQLogin.onClick {
            notEnabledLoginBtn()
            mPresenter.loginSocial(LOGIN_TYPE_QQ)
        }
        //微信登陆
        mIvWxLogin.onClick {
            notEnabledLoginBtn()
            mIsWXLogin = true
            mPresenter.loginSocial(LOGIN_TYPE_WX)
        }
        //微博登陆
        mIvWbLogin.onClick {
            notEnabledLoginBtn()
            mPresenter.loginSocial(LOGIN_TYPE_SINA)
        }
        //打开找回密码界面
        mTvPasswordRetrieval.onClick {
            ActivityHelper.startActivity(activity, SingleActivity::class.java, mapOf(
                    Pair(SingleActivity.EXTRA_NAME, SingleActivity.INTENT_EXTRA_REGISTER),
                    Pair(SingleActivity.BUNDLE_EXTRA_KEY_TYPE, RegisterFragment.TYPE_FORGET.toString())),
                    false)
        }
        //关闭当前事件
        mTvLoginCancel.setOnClickListener {
            activity.finish()
        }

        enabledLoginBtn()
    }

    private fun notEnabledLoginBtn() {
        mBtnLogin.enabled = false

        mDialog.show()
    }

    private fun enabledLoginBtn() {

        mDialog.dismiss()
        mBtnLogin.enabled = true
    }

    override fun setPresenter(presenter: LoginContract.ILoginPresenter) {
        mPresenter = presenter
    }

    override fun isActive(): Boolean {
        return isAdded
    }

    override fun getViewContext(): Context {
        return activity
    }

    override fun showNoNetwork() {
       // Toast.makeText(activity, "没有网络", Toast.LENGTH_LONG).show()
    }

    override fun showRefreshFailed() {
        ToastHelper.getInstance(activity).showError(getString(R.string.str_refresh_failed))
    }

    override fun showLoginSucceed(userInfo: IModel.IUserModel) {
        val intent = Intent(INTENT_LOGIN)     //登录成功，发送广播
        intent.putExtra(KEY_USER_INFO, userInfo)
        activity.sendBroadcast(intent)
        ToastHelper.getInstance(activity).showSucceed("登录成功")
        enabledLoginBtn()
        if (!mIsWXLogin)
            ActivityHelper.backActivity(activity)
    }

    override fun showLoginFailed() {
        ToastHelper.getInstance(activity).showError("登录失败")
        enabledLoginBtn()
        mIsWXLogin = false
    }

    override fun onStart() {
        super.onStart()
        mPresenter.startLoad()
    }

    override fun onResume() {
        super.onResume()
        if (mIsWXLogin)
            ActivityHelper.backActivity(activity)
    }

    companion object {
        val LOGIN_TYPE_QQ = LoginRepository.TYPE_LOGIN_QQ // 腾讯 QQ
        val LOGIN_TYPE_WX = LoginRepository.TYPE_LOGIN_WX // 微信
        val LOGIN_TYPE_SINA = LoginRepository.TYPE_LOGIN_SINA // 新浪微博

        /**
         * 获取实例
         */
        fun getInstance(): LoginFragment {
            return LoginFragment()
        }
    }
}